package com.ruiysoft.service.order.facade;

import com.alibaba.fastjson.JSON;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruiysoft.controller.wx.api.InnerWechatApi;
import com.ruiysoft.controller.wx.dto.OrderPayDto;
import com.ruiysoft.controller.wx.res.JsSignResponse;
import com.ruiysoft.entity.order.OrderMain;
import com.ruiysoft.entity.order.OrderPayTransaction;
import com.ruiysoft.entity.order.OrderSkuInfo;
import com.ruiysoft.pub.core.Result;
import com.ruiysoft.service.order.IOrderPayTransactionService;
import com.ruiysoft.util.CodeGenerator;
import com.ruiysoft.util.CurrencyUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Date : 2018/12/1
 *
 * @author WenJialu
 */
@Component
public class OrderCusPayFacade {
    @Resource
    IOrderPayTransactionService orderPayTransactionService;
    @Resource
    InnerWechatApi innerWechatApi;
    @Resource
    OrderPubFacade orderPubFacade;


    public OrderPayTransaction getOrderPayTransaction(String orderNo) {
        return Optional.ofNullable(orderNo)
                .map(no ->
                        orderPayTransactionService
                                .getOne(new QueryWrapper<OrderPayTransaction>()
                                        .eq("order_no", orderNo)
                                        .orderBy(false,false,"create_time")
                                        .last(" LIMIT 1 ")))
                .orElse(null);
    }

    public Result<JsSignResponse> generateOrderPayTransaction(OrderMain orderMain) {
        String sysTransactionNo = CodeGenerator.INSTANCE.genOrderPaySysTransactionNo();
        Result<JsSignResponse> jsSignResult = getJsSign(orderMain, sysTransactionNo);
        if (jsSignResult.isSuccess()) {
            OrderPayTransaction record = new OrderPayTransaction()
                    .setOrderId(orderMain.getId())
                    .setOrderNo(orderMain.getOrderNo())
                    .setUserId(orderMain.getUserId())
                    .setShopId(orderMain.getShopId())
                    .setOpenId(orderMain.getOpenId())
                    .setPayAmount(orderMain.getReceivableAmount())
                    .setSysTransactionNo(sysTransactionNo)
                    .setJsSign(JSON.toJSONString(jsSignResult.getData()))
                    .setPayType(orderMain.getPayType())
                    .setCreateTime(new Date());

            orderPayTransactionService.save(record);
        }
        return jsSignResult;
    }

    public Result<JsSignResponse> getJsSign(OrderMain orderMain, String transactionNo) {
        List<OrderSkuInfo> orderSkuInfos = orderPubFacade.getOrderSkuInfos(orderMain.getOrderNo());
        String productDesc = "";
        if (!orderSkuInfos.isEmpty()) {
            productDesc = orderSkuInfos.get(0).getMerchandiseName() + "等商品";
        }
        return innerWechatApi
                .jssign(new OrderPayDto()
                        .setShopId(orderMain.getShopId())
                        .setPersonnelId(orderMain.getUserId())
                        .setTransactionNo(transactionNo)
                        .setPayAmount(CurrencyUtils.INSTANCE.cent2YuanDecimal(orderMain.getReceivableAmount()))
                        .setProductDesc(productDesc));

    }
}
